
// 云函数入口文件
const cloud = require('wx-server-sdk');
const request = require('request');
cloud.init()
let appid = 'xxxxxxxxxxxxxxxxxxxxxx';//微信公众号开发者id
let secret = 'xxxxxxxxxxxxxxxxxxxxx';//微信公众号开发者secret_key
// 云函数入口函数
exports.main = async (event, context) => {
  console.log("获取access_token的appid为：" + appid)
  let token_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + appid + '&' + 'secret=' + secret;
  const rp = options =>
    new Promise((resolve, reject) => {
      request(options, (error, response, body) => {
        if (error) {
          reject(error);
        }
        resolve(response);
      });
    });
  const result = await rp({
    url: token_url,
    method: 'GET'
  });
  console.log("获取access_token的结果为：" + result.body);
  return (typeof result.body === 'object') ? result.body : JSON.parse(result.body);
}